package controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import interceptor.UserLoginInterceptor;
import model.Purchases;
import model.User;
import service.IPurchasesService;
import service.impl.PurchasesServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ykj on 16/1/11.
 */
public class PurchasesController extends BaseController {

    public void addDialog(){
        render("/admin/purchasesAddDialog.jsp");
    }

    public void updateDialog() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        Purchases purchases = (Purchases) purchasesService.findById(id);
        setAttr("purchases",purchases);
        render("/admin/purchasesUpdateDialog.jsp");
    }

    public void save() {
        Purchases purchases = getModel(Purchases.class);
        System.out.println(purchases.toJson());
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        Purchases result = (Purchases) purchasesService.save(purchases);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void update() {
        Purchases product = getModel(Purchases.class);
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        Purchases result = (Purchases) purchasesService.update(product);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void delete() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        boolean result = purchasesService.delete(id);
        renderDelResult(result);
    }

    public void getColumns() {
        List<String> fields = new ArrayList<String>() {{
            add("id");
            add("user_name");
            add("product_name");
            add("invest");
            add("create_time");
            add("valid_date");
            add("state");
            add("redeem_time");
        }};
        List<String> titles = new ArrayList<String>() {{
            add("标识");
            add("用户名称");
            add("产品名称");
            add("购买资金");
            add("购买日期");
            add("有效时长");
            add("购买状态");
            add("赎回时间");
        }};
        renderColumns(fields, titles);
    }

    public void purchases(){
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        renderData(purchasesService);
    }

    @Before(UserLoginInterceptor.class)
    public void detail(){
        User user = (User) getSession().getAttribute("user");
        if(user==null){
            renderError(401);
            return;
        }
        String pageStr = getPara("page");
        String rowsStr = getPara("rows");
        int page = pageStr!=null?Integer.parseInt(pageStr):1;
        int rows = rowsStr!=null?Integer.parseInt(rowsStr):20;
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        Page purchases = purchasesService.findByUserId(user.getId(), page, rows);
        setAttr("purchases",purchases);
        render("/front/purchase.jsp");
    }

    public void redeem(){
        Integer id = getParaToInt("id");
        User user = (User) getSession().getAttribute("user");
        if(id==null || user==null){
            renderError(401);
            return;
        }
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        double earning = 0;
        try {
            earning = purchasesService.redeem(user.getId(), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("earning",earning);
        renderJson(map);
    }

    public void monthDataByUser(){
        User user = (User) getSession().getAttribute("user");
        if(user==null){
            renderError(401);
            return;
        }
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        List<Double> buyData = purchasesService.monthBuyByUser(user.getId());
        List<Double> redeemData = purchasesService.monthRedeemByUser(user.getId());
        List<List> all = new ArrayList<List>();
        all.add(buyData);
        all.add(redeemData);
        renderJson(all);
    }

}
